#include "event.h"

#define	MESSAGE_ALLOC	256
#define ADDRESS_ALLOC	48

typedef struct		mouse_info_t
{
  unsigned int	x;
  unsigned int	y;
}			mouse_info_t;

typedef struct		program_info_t
{
  unsigned int	cptmessage;
  unsigned int	cptaddress;
  unsigned int	cptkey;
  char*		message;
  char*		address;
  char*		console;
  char*		add_key;
  SDL_Surface*	screen;
  SDL_Surface*	background;
}			program_info_t;

static int validate(program_info_t*	prog_info)
{
  char*			printty = NULL;
  char*			printty2 = NULL;
  int			res = 1;
  printty = malloc(100 * sizeof (char));
  sprintf(printty,"cp %s temp.wav", prog_info->address);
  system(printty);
  printty2 = malloc(100 * sizeof (char));
  sprintf(printty2,"cp %s cle_publique", prog_info->add_key);
  system(printty2);
  if ((prog_info->cptmessage != 0) && (prog_info->cptaddress != 0) && (prog_info->cptkey != 0))
  {
    res = stegano(prog_info->message);
    if (res > 0)
      res = 5;
  }
  else
    res = 12;
  return (res );
}


static int dechiffre(program_info_t*	prog_info)
{
  char	*printy = NULL;
  char	*printty = NULL;
  int	res = 1;

  printty = malloc(100 * sizeof (char));
  sprintf(printty,"cp %s temp.wav", prog_info->address);
  system(printty);
  printy = malloc(100 * sizeof (char));
  sprintf(printy, "cp %s cle_privee", prog_info->add_key);
  system(printy);

  if ((prog_info->cptaddress != 0) && (prog_info->cptkey != 0))
  {
    res = destegano();
    if (res > 0)
      res = 6;
  }
  else
    res = 13;
  return (res);
}


static void		info_init(program_info_t*		prog_info,
				  SDL_Surface*			screen,
				  SDL_Surface*			background)
{
  prog_info->cptmessage = 0;
  prog_info->cptaddress = 0;
  prog_info->cptkey = 0;
  prog_info->screen = screen;
  prog_info->background = background;

  prog_info->message = calloc(256, sizeof (char));
  prog_info->address = calloc(48, sizeof (char));
  prog_info->console = calloc(128, sizeof (char));
  prog_info->add_key = calloc(48, sizeof (char));
  SDL_EnableUNICODE(1);
}

static void             prog_init(program_info_t*               prog_info)
{
  prog_info->cptmessage = 0;
  prog_info->cptaddress = 0;
  prog_info->cptkey = 0;

  prog_info->message = NULL;
  prog_info->address = NULL;
  prog_info->console = NULL;
  prog_info->add_key = NULL;
  prog_info->message = calloc(256, sizeof (char));
  prog_info->address = calloc(48, sizeof (char));
  prog_info->console = calloc(128, sizeof (char));
  prog_info->add_key = calloc(48, sizeof (char));

}

static void		mouse_move(SDL_Event*			event,
				   mouse_info_t*		mouse_info)
{
  char			title[100];

  mouse_info->x = event->motion.x;
  mouse_info->y = event->motion.y;
/*  snprintf(title, 99, "Mouse x:%i y:%i", mouse_info->x, mouse_info->y);*/
  snprintf(title, 99, "Steganodio");
  SDL_WM_SetCaption(title, NULL);
}

static void             go_to_zero(program_info_t*      prog_info)
{
  prog_init(prog_info);
  prog_info->message[prog_info->cptmessage] = '\0';
  prog_info->address[prog_info->cptaddress] = '\0';
  interface_write(NULL, NULL,NULL,prog_info->background, prog_info->screen,NULL );
}

static int		mouse_get_area(program_info_t*		prog_info,
				       mouse_info_t*		mouse_info)
{
  int res = 1;

  /* Chiffre button */
  if ((mouse_info->x > 110) && (mouse_info->x < 250) &&
      (mouse_info->y > 510) && (mouse_info->y < 530))
  {
    res = validate(prog_info);
    if (res == 5)
      go_to_zero(prog_info);
  }

  /* Cancel button */
  if ((mouse_info->x > 290) && (mouse_info->x < 420) &&
      (mouse_info->y > 510) && (mouse_info->y < 530))
    go_to_zero(prog_info);

  /*Dechiffre button*/
  if ((mouse_info->x > 460) && (mouse_info->x < 590) &&
      (mouse_info->y > 510) && (mouse_info->y < 530))
  {
    res = dechiffre(prog_info);
      if (res == 6)
	go_to_zero(prog_info);
  }
  return (res);
}

static void		keyboard_msg_area(SDL_Event*		event,
					  program_info_t*	prog_info)
{
  if (prog_info->cptmessage < 255)
  {
    if ((prog_info->cptmessage != 0) && (prog_info->cptmessage % 50 == 0))
    {
      prog_info->message[prog_info->cptmessage] = '\n';
      prog_info->cptmessage++;
    }
    if ((event->key.keysym.unicode == 32) || (event->key.keysym.unicode >= 40))
    {
      prog_info->message[prog_info->cptmessage] = event->key.keysym.unicode;
      interface_write(prog_info->message, prog_info->address,prog_info->add_key,
		      prog_info->background, prog_info->screen,prog_info->console);
      prog_info->cptmessage++;
    }
  }

  if (prog_info->cptmessage && event->key.keysym.sym == SDLK_BACKSPACE)
  {
    prog_info->cptmessage--;
    prog_info->message[prog_info->cptmessage] = '\0';
    interface_write(prog_info->message, prog_info->address,prog_info->add_key,
		    prog_info->background, prog_info->screen,prog_info->console);
  }
}


static int		keyboard_address_area(SDL_Event*	event,
					      program_info_t*	prog_info)
{
  if (prog_info->cptaddress < 47)
  {
    if ((event->key.keysym.unicode == 52) || (event->key.keysym.unicode >= 40))
    {
      prog_info->address[prog_info->cptaddress] = event->key.keysym.unicode;
      interface_write(prog_info->message, prog_info->address,prog_info->add_key,
		      prog_info->background, prog_info->screen,prog_info->console);
      prog_info->cptaddress++;
    }
  }
  if  (prog_info->cptaddress > 0)
  {
    switch (event->key.keysym.sym)
    {
      case SDLK_BACKSPACE:
	prog_info->cptaddress--;
	prog_info->address[prog_info->cptaddress] = '\0';
	interface_write(prog_info->message, prog_info->address,prog_info->add_key,
			prog_info->background, prog_info->screen,prog_info->console);
	break;

      default:
	break;
    }
  }
  return 0;
}

static int		keyboard_key_area(SDL_Event*	event,
					      program_info_t*	prog_info)
{
  if (prog_info->cptkey < 47)
  {
    if ((event->key.keysym.unicode == 52) || (event->key.keysym.unicode >= 40))
    {
      prog_info->add_key[prog_info->cptkey] = event->key.keysym.unicode;
      interface_write(prog_info->message, prog_info->address,prog_info->add_key,
		      prog_info->background, prog_info->screen,prog_info->console);
      prog_info->cptkey++;
    }
  }
  if  (prog_info->cptkey > 0)
  {
    switch (event->key.keysym.sym)
    {
      case SDLK_BACKSPACE:
	prog_info->cptkey--;
	prog_info->add_key[prog_info->cptkey] = '\0';
	interface_write(prog_info->message, prog_info->address,prog_info->add_key,
			prog_info->background, prog_info->screen,prog_info->console);
	break;

      default:
	break;
    }
  }
  return 0;
}


static int		keyboard_manage(SDL_Event*		event,
					program_info_t*		prog_info,
					mouse_info_t*		mouse_info)
{
  int res = 1;

  if ((mouse_info->x > 7) && (mouse_info->x < 780) &&
      (mouse_info->y > 190) && (mouse_info->y < 460))
    keyboard_msg_area(event, prog_info);

  if ((mouse_info->x > 390) && (mouse_info->x < 780) &&
      (mouse_info->y > 110) && (mouse_info->y < 140))
    keyboard_address_area(event, prog_info);

  if ((mouse_info->x > 390) && (mouse_info->x < 780) &&
      (mouse_info->y > 150) && (mouse_info->y < 160))
    keyboard_key_area(event, prog_info);

  if (event->key.keysym.sym == SDLK_RETURN)
  {
    res = validate(prog_info);
    if (res  == 5)
      go_to_zero(prog_info);
  else
    res = validate(prog_info);
  }
  if (event->key.keysym.sym == SDLK_ESCAPE)
    return 0;
  return res;
}

static int		event_manage(SDL_Event*			event,
				     program_info_t*		prog_info,
				     mouse_info_t*		mouse_info)
{
  SDL_WaitEvent(event);

  switch (event->type)
  {
    case SDL_MOUSEMOTION:
      mouse_move(event, mouse_info);
      break;

    case SDL_MOUSEBUTTONUP:
      return (mouse_get_area(prog_info, mouse_info));

    case SDL_KEYDOWN:
      return (keyboard_manage(event, prog_info, mouse_info));
      break;

    case SDL_QUIT:
      return (0);
  }
  return (1);
}


void			event2(SDL_Surface*		screen,
			      SDL_Surface*		background)
{
  int			stop = 42;
  SDL_Event		event;
  program_info_t	prog_info;
  mouse_info_t		mouse_info;

  info_init(&prog_info, screen, background);
  while (stop != 0)
  {
    stop = event_manage(&event, &prog_info, &mouse_info);
    if (stop == 12)
    {
      prog_info.console = "Veuillez renseigner tous les champs (message et adresse)";
      interface_write(prog_info.message, prog_info.address,prog_info.add_key,
		      prog_info.background, prog_info.screen,prog_info.console);
    }
    if (stop == 5)
    {
      prog_info.console = "Le texte a bien ete implemente dans le fichier son";
      interface_write(prog_info.message, prog_info.address,prog_info.add_key,
		      prog_info.background, prog_info.screen,prog_info.console);
    }
    if (stop == 6)
    {
      prog_info.console = "Le fichier son a bien ete dechiffre";
      interface_write(prog_info.message, prog_info.address,prog_info.add_key,
		      prog_info.background, prog_info.screen,prog_info.console);
    }
    if(stop == 13)
    {
      prog_info.console = "Veuillez renseigner les champs d'adresses";
      interface_write(prog_info.message, prog_info.address,prog_info.add_key,
		      prog_info.background, prog_info.screen,prog_info.console);
    }
    if (stop < 0)
    {
      prog_info.console = "Le fichier son n'est pas au bon format";
      interface_write(prog_info.message, prog_info.address,prog_info.add_key,
		      prog_info.background, prog_info.screen,prog_info.console);
    }
  }
  printf("Merci d'avoir utiliser steganodio\n");
  sleep(1);
}
